﻿using log4net;
using System;
using System.Collections;

namespace Velocity4Net.Util.Introspection
{

    /*
     * Licensed to the Apache Software Foundation (ASF) under one
     * or more contributor license agreements.  See the NOTICE file
     * distributed with this work for additional information
     * regarding copyright ownership.  The ASF licenses this file
     * to you under the Apache License, Version 2.0 (the
     * "License"); you may not use this file except in compliance
     * with the License.  You may obtain a copy of the License at
     *
     *   http://www.apache.org/licenses/LICENSE-2.0
     *
     * Unless required by applicable law or agreed to in writing,
     * software distributed under the License is distributed on an
     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
     * KIND, either express or implied.  See the License for the
     * specific language governing permissions and limitations
     * under the License.
     */










    /**
     * This is the internal introspector cache implementation.
     *
     * @author <a href="mailto:henning@apache.org">Henning P. Schmiedehausen</a>
     * @author <a href="mailto:cdauth@cdauth.eu">Candid Dauth</a>
     * @version $Id: IntrospectorCache.java 1855206 2019-03-11 11:09:53Z cbrisson $
     * @since 1.5
     */
    public sealed class IntrospectorCache
    {
        /**
         * define a public string so that it can be looked for if interested
         */
        public const String CACHEDUMP_MSG =
                "IntrospectorCache detected classloader change. Dumping cache.";

        /** Class logger */
        private readonly ILog log;

        /**
         * Holds the method maps for the classes we know about. Map: Class --&gt; ClassMap object.
         */
        private readonly IDictionary classMapCache = new Hashtable();

        /**
         * Holds the field maps for the classes we know about. Map: Class --&gt; ClassFieldMap object.
         */
        private readonly IDictionary classFieldMapCache = new Hashtable();

        /**
         * Keep the names of the classes in another map. This is needed for a multi-classloader environment where it is possible
         * to have Class 'Foo' loaded by a classloader and then get asked to introspect on 'Foo' from another class loader. While these
         * two Class objects have the same name, a <code>classMethodMaps.get(typeof(Foo))</code> will return null. For that case, we
         * keep a set of class names to recognize this case.
         */
        private readonly ISet classNameCache = new HashSet();

        /**
         * Conversion handler
         */
        private readonly TypeConversionHandler conversionHandler;

        /**
         * C'tor
         * @param log logger.
         * @param conversionHandler conversion handler
         */
        public IntrospectorCache(ILog log, TypeConversionHandler conversionHandler)
        {
            this.log = log;
            this.conversionHandler = conversionHandler;
        }

        /**
         * Clears the internal cache.
         */
        public void clear()
        {
            lock (classMapCache)
            {
                classMapCache.clear();
                classFieldMapCache.clear();
                classNameCache.clear();
                log.Debug(CACHEDUMP_MSG);
            }
        }

        /**
         * Lookup a given Class object in the cache. If it does not exist,
         * check whether this is due to a class change and purge the caches
         * eventually.
         *
         * @param c The class to look up.
         * @return A ClassMap object or null if it does not exist in the cache.
         */
        public ClassMap get(Class c)
        {
            ClassMap classMap = (ClassMap)classMapCache.get(Validate.notNull(c));
            if (classMap == null)
            {
                /*
                 * check to see if we have it by name.
                 * if so, then we have an object with the same
                 * name but loaded through a different class loader.
                 * In that case, we will just dump the cache to be sure.
                 */
                lock (classMapCache)
                {
                    if (classNameCache.Contains(c.getName()))
                    {
                        clear();
                    }
                }
            }
            return classMap;
        }

        /**
         * Lookup a given Class object in the cache. If it does not exist,
         * check whether this is due to a class change and purge the caches
         * eventually.
         *
         * @param c The class to look up.
         * @return A ClassFieldMap object or null if it does not exist in the cache.
         */
        public ClassFieldMap getFieldMap(Class c)
        {
            ClassFieldMap classFieldMap = (ClassFieldMap)classFieldMapCache.get(Validate.notNull(c));
            if (classFieldMap == null)
            {
                /*
                 * check to see if we have it by name.
                 * if so, then we have an object with the same
                 * name but loaded through a different class loader.
                 * In that case, we will just dump the cache to be sure.
                 */
                lock (classMapCache)
                {
                    if (classNameCache.Contains(c.getName()))
                    {
                        clear();
                    }
                }
            }
            return classFieldMap;
        }

        /**
         * Creates a class map for specific class and registers it in the
         * cache.  Also adds the qualified name to the name-&gt;class map
         * for later Classloader change detection.
         *
         * @param c The class for which the class map gets generated.
         * @return A ClassMap object.
         */
        public ClassMap put(Class c)
        {
            ClassMap classMap = new ClassMap(c, log, conversionHandler);
            ClassFieldMap classFieldMap = new ClassFieldMap(c, log);
            lock (classMapCache)
            {
                classMapCache.put(c, classMap);
                classFieldMapCache.put(c, classFieldMap);
                classNameCache.Add(c.getName());
            }
            return classMap;
        }

    }
}